package com.app.ui.activity.account;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.app.net.res.user.User;
import com.app.ui.activity.action.NormalActionBar;
import com.app.ui.event.UserEvent;
import com.app.ui.pager.home.HomeMePager;
import com.app.utiles.other.ToastUtile;
import com.wxj.zx.R;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.listener.UpdateListener;


/**
 * 修改昵称
 * Created by Administrator on 2016/2/15.
 */
public class UserNameActivity extends NormalActionBar {

    @Bind(R.id.name_et)
    EditText nameEt;
    private String name;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_name);
        setBarColor();
        ButterKnife.bind(this);
        setBarBack();
        setBarTvText(1, "修改姓名");
        setBarTvText(2, "保存");
        user = baseApplication.getUser();
        name = user.getUserNickName();
        nameEt.setHint(name);
    }

    @Override
    protected void option() {
        String n = nameEt.getText().toString();
        if (TextUtils.isEmpty(n)) {
            ToastUtile.showToast("请输入姓名");
            return;
        }
        if (n.equals(name)) {
            ToastUtile.showToast("请修改姓名");
            return;
        }
        user.setUserNickName(n);
        dialogShow();
        user.update(this, new Listener());
    }

    class Listener extends UpdateListener {
        @Override
        public void onSuccess() {
            dialogDismiss();
            baseApplication.setUser(user);
            ToastUtile.showToast("修改成功");
            UserEvent userEvent = new UserEvent();
            userEvent.setClsName(UserUpdateActivity.class, HomeMePager.class);
            EventBus.getDefault().post(userEvent);
            finish();
        }

        @Override
        public void onFailure(int i, String s) {
            dialogDismiss();
            ToastUtile.showToast("修改失败");
        }
    }
}
